package czy.demo.listener;

import com.rabbitmq.client.Channel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

/**
 * 默认队列的监听器
 * 一个单独创建的队列,在没有绑定到任何exchange时,会自动绑定到默认exchange
 * 默认exchange以空字符串指代,以队列名词为routingKey
 */
@Component
public class DefaultListener {

    private static final Logger logger = LoggerFactory.getLogger(DefaultListener.class);

    /**
     *  监听单独创建的队列,需要使用queues属性指定队列名称,此时默认exchange为空字符串,key为队列名
     *  不能使用queues来监听绑定到具名exchange的队列
     */
    @RabbitListener(
            queues = {"default"}
    )
    public void onMessage(Message message, Channel channel) throws Exception {
        logger.info("接收到Default队列消息: ");
        logger.info("properties: "+message.getMessageProperties());
        logger.info("body: "+new String(message.getBody()));
    }
}
